package online.dgyj.rgb;

import android.util.Log;

import com.nle.mylibrary.protocolEntity.DataProtocol;
import com.nle.mylibrary.util.DataTools;

public class FindRGB extends DataProtocol {

    private byte[] command;

    public byte[] buildRequestCommand() {
        Log.e("test", "led " + DataTools.formatByteArray(this.command));
        return this.command;
    }

    public FindRGB(int rgbAddress) throws Exception {
        byte address = DataTools.Str2ByteArray(DataTools.int2HexStr(rgbAddress))[0];
        byte[] temp = new byte[]{address, 3, 0, 0, 0, 4};
        byte[] crc16 = DataTools.calculateCrc16LowHight(temp);
        byte[] realCommond = new byte[temp.length + crc16.length];
        System.arraycopy(temp, 0, realCommond, 0, temp.length);
        System.arraycopy(crc16, 0, realCommond, temp.length, crc16.length);
        this.command = realCommond;
    }

    public void receiveMsg(byte[] data, int len) {
        this.printLogData(data, len, "接受的数据");
    }

    void printLogData(byte[] receiveBytes, int len, String logPre) {
        StringBuilder stringBuffer = new StringBuilder();

        for (int i = 0; i < len; ++i) {
            byte itemByte = receiveBytes[i];
            String hexString = Integer.toHexString(itemByte & 255);
            if (hexString.length() == 1) {
                hexString = "0" + hexString;
            }

            stringBuffer.append(hexString).append(" ");
        }

        Log.e(logPre + ">>>", stringBuffer.toString());
    }
}
